package com.jinkeen.xiaxie.customView

import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.core.graphics.withTranslation
import com.jinkeen.xiaxie.R

class TwoCircleView constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context, attributeSet), AnimatorUpdateListener {
    private var mWidth = 0
    private var mHeight = 0

    //亮光开始角度
    private var mainStartAngle = 8f


    //内院路径
    private var lightPath = Path()

    //动画当前值
    private var value = 0f

    //声明动画
    private var animator: ValueAnimator = ValueAnimator.ofFloat(1f, 15f)

    /**
     * 环画笔
     */
    private var mCirclePaint = Paint().apply {
        style = Paint.Style.STROKE
        strokeWidth = 10f
        isAntiAlias = true
        isDither = true
        color = Color.parseColor("#1B676C")
    }

    private var lightPaint = Paint().apply {
        style = Paint.Style.STROKE
        strokeWidth = 15f
        isAntiAlias = true
        isDither = true
        strokeCap = Paint.Cap.ROUND
        color = Color.parseColor("#1FB6C0")
    }


    init {
        initAnimator()
    }

    private fun initAnimator() {
        animator.apply {
            duration = 15000
            //无重复
            repeatCount = ValueAnimator.INFINITE
            //重复模式-从头开始
            repeatMode = ValueAnimator.RESTART
        }
        animator.addUpdateListener {
            //获取当前值
            value = (it.animatedValue) as Float
            invalidate()

        }
        //设置插值器
        animator.interpolator = LinearInterpolator()
//        animator.start()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //将画布移动到屏幕中央
        canvas?.withTranslation(width / 2f, height / 2f) {
            drawTwoCircle(this)
            drawLight(this)
        }
    }

    private fun drawLight(canvas: Canvas?) {
        lightPath = Path()
        val rectF = RectF()
        rectF.apply {
            left = -100f
            right = 100f
            top = -100f
            bottom = 100f
        }
//        lightPath.addArc(rectF, 30 * value, 60f)
//        canvas?.drawPath(lightPath, lightPaint)
    }

    private fun drawTwoCircle(canvas: Canvas?) {
        canvas?.drawCircle(0f, 0f, 60f, mCirclePaint)
        mCirclePaint.color = Color.parseColor("#1FB6C0")
        canvas?.drawCircle(0f, 0f, 100f, mCirclePaint)

        val rectF = RectF()
        rectF.apply {
            left = 30f
            right = 30f
            top = 30f
            bottom = 30f
        }
//        lightPath.addArc(rectF, 200f, 10f)
//        canvas?.drawPath(lightPath, mCirclePaint)
    }

    override fun onAnimationUpdate(animation: ValueAnimator?) {

    }
}